<?php
header("Content-Type: text/html;charset=utf-8");

$coon = mysqli_connect('localhost', 'root', 'root', 'git');
$sql = "select * from menu";
$data= mysqli_query($coon, $sql);
$arr = mysqli_fetch_all($data);
   // var_dump($arr);

function tree($arr,$pid=0,$level=1){
   //var_dump(111);
    static $tree=array();
    foreach ($arr as $v){
        if($v[2]==$pid){
            //array_push($data,$arr);
            //var_dump($data);
            $v[3]=$level;
            $tree[]=$v;
            tree($arr,$v[0],$level+1);
           // echo 'end';
        }
    }
    return $tree;

}
function exist_son($val,$arr){
    foreach ($arr as $v){
        if($v[2]==$val)
            return true;
    }
    return false;
}
$arr=tree($arr);
//var_dump($arr);

